package com.li.client.management;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.li.client.entities.BookInfo;
import com.li.client.entities.User;
import com.li.client.service.DataServiceFactory;
import com.li.client.util.AppStyle;
import com.li.client.util.GwtCallBack;
import com.li.client.util.StringUtilGwt;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class BorrowBookManager extends VLayout {
	private DynamicForm form;
	private Button borrowBtn;
	private VLayout layout;

	private TextItem isbnText;
	private TextItem userIdText;
	
	public BorrowBookManager()
	{
//		addMember(new Label("This is borrow book screen"));
		initContent();
	}
	
	private DynamicForm createDefaultForm()
	{
		DynamicForm formLocal = new DynamicForm();
		formLocal.setWidth(AppStyle.px90);
		formLocal.setHeight(AppStyle.px50);
		formLocal.setPadding(AppStyle.px3);
		formLocal.setNumCols(AppStyle.px3);
		formLocal.setCanSelectText(Boolean.TRUE);
//		formLocal.setBorder("2px solid black");
		return formLocal;
	}

	public void initContent()
	{
		form = createDefaultForm();
		
		isbnText = new TextItem("isbnField", "ISBN code");
		isbnText.setWrapTitle(false);
		
		userIdText = new TextItem("userIdField", "User");

		form.setFields(userIdText, isbnText);
		
		borrowBtn = new Button("Ok");
//		borrowBtn.setTop(50);
//		borrowBtn.setBorder("2px solid red");
		borrowBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				final String userId;
				final String isbn;
				if (userIdText.getValue() == null || isbnText.getValue() == null) {
					SC.say("User or ISBN should not be empty!");
					return;
				} else {
					userId = userIdText.getValueAsString().trim();
					isbn = isbnText.getValueAsString().trim();
					if (!StringUtilGwt.isNotBlank(userId) || !StringUtilGwt.isNotBlank(isbn)) {
						SC.say("User or ISBN should not be empty!");
						return;
					}
				}
				
				DataServiceFactory.getService().getUserByUserId(userId, new AsyncCallback<User>() {

					@Override
					public void onFailure(Throwable caught) {
						SC.say(caught.getMessage());
					}

					@Override
					public void onSuccess(User user) {
						if (user == null) {
							SC.say("user Id " + userId + " is not a valid");
						} else {
							DataServiceFactory.getService().getBookByISBN(isbn, new AsyncCallback<BookInfo>() {

								@Override
								public void onFailure(Throwable caught) {
									SC.say(caught.getMessage());
								}

								@Override
								public void onSuccess(BookInfo bookInfo) {
									if (bookInfo == null) {
										SC.say("the isbn code " + isbn + " is not valid");
									} else {
										DataServiceFactory.getService().getBookInStock(isbn, new AsyncCallback<Integer>() {

											@Override
											public void onFailure(
													Throwable caught) {
												SC.say(caught.getMessage());
											}

											@Override
											public void onSuccess(Integer inStock) {
												if (inStock == null || inStock <= 0) {
													SC.say("the book is out of stock");
												} else {
													DataServiceFactory.getService().borrowBook(userId, isbn, new AsyncCallback<Boolean>() {

														@Override
														public void onFailure(
																Throwable caught) {
															SC.say("Borrow book fail");
														}
														
														@Override
														public void onSuccess(Boolean result) {
															if (result) {
																SC.say("Borrow book success");
															} else {
																SC.say("Borrow book fail");
															}
															
														}
													});
												}
											}
										});
									}
								}
							});
						}
					}
				});
				
				
			}}
		);
		
		layout = new VLayout();
		layout.addMember(form);
		layout.addMember(borrowBtn);
		
		addMember(layout);
		
	}
}
